﻿using System;
using System.Linq;
using System.Web;
using Grundy.Entity;
using Grundy.Message;

namespace Grundy.Administration.Web.Services
{
    public class FeatureService
    {
        public EntitlementsDatabase Db { get; private set; }

        public FeatureService()
        {
            Db = new EntitlementsDatabase("EntitlementsDatabase");
        }

        public FeatureService(EntitlementsDatabase db)
        {
            Db = db;
        }

        public IQueryable<Feature> GetFeatures(int page = 1, int pageSize = 0)
        {
            if(page == 1 && pageSize == 0)
                return Db.Features.OrderBy(i => i.Name);
            return Db.Features.OrderBy(i => i.Name).Skip((page - 1) * pageSize).Take(pageSize);
        }

        public Feature GetFeature(Guid id)
        {
            return Db.Features.Find(id);
        }

        public Feature GetFeature(string name)
        {
            return Db.Features.FirstOrDefault(f => f.Name == name);
        }

        public Feature CreateFeature(string name)
        {
            var feature = new Feature { Name = name };
            Db.Features.Add(feature);
            Db.SaveChanges();
            return feature; 
        }
    }
}